<template>
    <el-container class="home-container">
        <el-header>
            <div>
                <img src="../assets/logozbd.png" style="width: 40px;border-radius: 50%"/>
                <span>{{system_config.sys_name}}</span>
            </div>
            <div>


                <el-row :gutter="5">
<!--                    <el-col>-->
<!--                        <el-badge is-dot class="item">-->
<!--                            <i class="el-icon-chat-round"></i>-->
<!--                        </el-badge>-->
<!--                    </el-col>-->
                    <el-col>

                        <i class="el-icon-menu" @click="clickTag('/welcome')"></i>
                    </el-col>
                    <el-col>
                        <!--            用户名-->
                        <el-tooltip :content="cname?cname:username" placement="bottom" effect="light">
                            <el-avatar shape="square" size="small"
                                       src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"></el-avatar>

                        </el-tooltip>
                    </el-col>
                    <el-col>
                        <el-button @click="logout" type="info" size="mini">退出</el-button>
                    </el-col>
                </el-row>


                <div></div>


                <div></div>

            </div>
        </el-header>
        <el-container>
            <el-aside :width="isCollapse? '64px' :'200px'">
                <div class="toggle_button" @click="toggleCollapse">|||</div>
                <el-menu
                        background-color="#333744"
                        text-color="#fff"
                        active-text-color="#5f8b26"
                        unique-opened
                        :collapse="isCollapse"
                        :collapse-transition="false"
                        router
                        :default-active="activePath"
                >
                    <el-submenu :index="item.id+''" v-for='item in menu' :key="item.id">
                        <!--                        一级菜单的模板区-->
                        <template slot="title">
                            <i :class="item.icon"></i>
                            <span>{{item.authName}}</span>
                        </template>
                        <!--                        二级菜单-->
                        <el-menu-item @click="saveNavStatus('/'+subitem.path+'',subitem.authName)"
                                      v-for="subitem in item.children" :key="subitem.id" :index="'/'+subitem.path+''">
                            <template slot="title">
                                <i :class="subitem.icon"></i>
                                <span>{{subitem.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-main>
                <!--          主体页面显示区      -->

                <el-row class="borded" style="padding: 5px" v-show="tagVisibale">
                    <el-col>
                        <el-tag
                                v-for="tag in dynamicTags"
                                closable
                                :key="tag.path"
                                :disable-transitions="false"
                                @close="handleClose(tag)"
                                @click="clickTag(tag.path)">
                            {{tag.name}}
                        </el-tag>
                    </el-col>
                </el-row>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>
<script>
    import {Message} from 'element-ui'

    export default {
        name: "Home",
        data() {
            return {
                //是否有通知
                hasNotice: false,
                //标签状态
                dynamicTags: [],
                tagVisibale: false,
                //菜单数据
                menu: [],
                activePath: '',
                iconObj: {
                    '125': 'el-icon-menu',
                    '103': 'el-icon-menu',
                    '101': 'el-icon-menu',
                    '102': 'el-icon-menu',
                    '145': 'el-icon-menu',
                    '160': 'el-icon-menu',
                    '161': 'el-icon-menu',
                    '162': 'el-icon-menu'
                },
                isCollapse: false,
                username: '',
                cname:'',
                system_config: ''
            }
        },
        created() {
            this.getMenuList()
            this.activePath = window.sessionStorage.getItem('activePath')
            let pathname = window.sessionStorage.getItem('activeName')

            this.username = this.$store.state.username?this.$store.state.username:window.sessionStorage.getItem('username')
            this.cname = this.$store.state.cname?this.$store.state.cname:window.sessionStorage.getItem('cname')

            this.getSysConfig()
            if (pathname) {
                this.tagVisibale = true;
                this.$store.commit('addTagLabel', {name: pathname, path: this.activePath})
            }
            //获取tablabel标签
            this.dynamicTags = this.$store.state.tagLabel

        },
        methods: {

            clickTag(path) {

                this.$router.push(path);
                //侧边栏联动效果
                this.activePath = path;
                if(path =='/welcome'){

                    this.tagVisibale=false
                    // this.$store.commit('clearAllTagLabel')
                    // this.dynamicTags=[]

                }

            },

            handleClose(tag) {
                let index = this.dynamicTags.indexOf(tag)
                if (index > 0) {
                    this.$router.push(this.dynamicTags[index - 1].path)
                    this.activePath = this.dynamicTags[index - 1].path
                }
                if (index == 0 && this.dynamicTags.length > 1) {
                    this.$router.push(this.dynamicTags[index + 1].path)
                    this.activePath = this.dynamicTags[index + 1].path
                }
                if (index == 0 && this.dynamicTags.length == 1) {
                    this.tagVisibale = false
                    this.$router.push('/welcome')
                    this.activePath = ''
                }


                this.dynamicTags.splice(index, 1);
                this.$store.commit('removeTagLabel', {path: tag.name, name: tag.path})


            },


            saveNavStatus(activePath, authName) {
                this.activePath = activePath
                this.tagVisibale = true;
                window.sessionStorage.setItem('activePath', activePath)
                window.sessionStorage.setItem('activeName', authName)
                this.$store.commit('addTagLabel', {name: authName, path: activePath})

            },
            //前端登出
            logout() {
                window.sessionStorage.clear()
                this.$store.commit('clearAllTagLabel')
                this.$router.push('/login')
            },

            async getMenuList() {
                let {data: res} = await this.$http.get('/menus')
                if (res.meta.status != 200) return Message.error(res.meta.msg)
                this.menu = res.data
                console.log(res)
            }
            , toggleCollapse() {
                this.isCollapse = !this.isCollapse
            },
            async getSysConfig() {
                let {data: res} = await this.$http.get('sysconfig')
                console.log(res)
                if (res.meta.status != 200) this.system_config = {
                    sys_icon: "el",
                    sys_name: "管理系统"
                }
                this.system_config = res.data
            }
        }
    }
</script>

<style scoped lang="less">
    .home-container {
        height: 100%;
    }

    .el-header {
        background-color: #373d41;
        display: flex;
        justify-content: space-between;
        color: white;
        font-size: 20px;

        div {
            display: flex;
            align-items: center;

            span {
                margin-left: 15px;
            }
        }
    }

    .el-aside {
        background-color: #333744;
    }

    .el-main {
        background-color: #eaedf1;

        .el-tag {
            margin: 2px;
            cursor: pointer;
        }
    }

    .el-menu {
        border: 0;
    }

    .toggle_button {
        background-color: #203f55;
        font-size: 10px;
        line-height: 24px;
        color: #fff5d4;
        text-align: center;
        letter-spacing: 2px;
        pointer-events: auto;
    }
</style>
